﻿@{
    CurrentUser user = ViewBag.CurrentUser;
    string table = ViewBag.Table;
    IEnumerable<dynamic> fieldData = Html.GetFieldList(table, "list", user);
    IEnumerable<dynamic> allfieldListData = Html.GetFieldList(table, "", user);
}
<div class="current-location">
    <ul>
        <li class="left"><i class="glyphicon glyphicon-home"></i><a href="javascript:void(0)" onclick="location.href=location.href">会员管理</a></li>
        <li class="right"><a href="javascript:void(0)" onclick="LoadData()" title="刷新"><span class="glyphicon glyphicon-refresh"></span></a></li>
    </ul>
</div>

<div class="main">

    <ul class="navTabs">
        <li data-tab-params="tabName:'全部',tabValue:'',tabValueObjName:'#state'"></li>
        <li data-tab-params="tabName:'已审核',tabValue:'1',tabValueObjName:'#state'"></li>
        <li data-tab-params="tabName:'待审核',tabValue:'0',tabValueObjName:'#state'"></li>
        <li data-tab-params="tabName:'已禁用',tabValue:'-1',tabValueObjName:'#state'"></li>
    </ul>

    <table class="tb-head" cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td class="btn-group left">
                <button type="button" class="btn btn-default btn-sm ui-selectInvert">反选</button>
                <button type="button" class="btn btn-default dropdown-toggle btn-sm" data-toggle="dropdown" aria-expanded="false">
                    菜单
                    <span class="caret"></span>
                </button>
                <ul class="dropdown-menu" role="menu">
                    <li><a class="ui-dialog" href="javascript:void(0)" data-custom-params="title:'添加用户', url:'@Url.ActionUrl("Add")',width:'95%',height:'95%'">添加</a></li>
                    <li class="divider"></li>
                    <li><a class="ui-setMultiple" data-custom-params="title:'用户组设置', url:'@Url.ActionUrl("MemberGroupSet")',width:'500px',height:'90%'" href="javascript:void(0)">用户组设置</a></li>
                    <li><a class="ui-setMultiple" data-custom-params="title:'部门设置', url:'@Url.ActionUrl("DepartmentSet")',width:'500px',height:'90%'" href="javascript:void(0)">部门设置</a></li>
                    <li class="divider"></li>

                    <li><a href="javascript:void(0)" class="ui-setField" data-custom-params="url:'@Url.ActionUrl("SetField")',setField:'state',setValue:'1',callBack:'LoadData'">审核</a></li>

                    <li><a href="javascript:void(0)" class="ui-setField" data-custom-params="url:'@Url.ActionUrl("SetField")',setField:'state',setValue:'-1',callBack:'LoadData'">禁用</a></li>
                    <li class="divider"></li>
                    <li><a href="javascript:void(0)" class="ui-deleteMultiple" data-custom-params="url:'@Url.ActionUrl("Delete")',beforeExecute:'BeforeExecuting',callBack:'LoadData'">删除</a></li>
                </ul>
            </td>
            <td class="right form-group-sm">
                <div class="controls form-inline">
                    <select id="memberGroupId" class="ui-queryString form-control ui-selectBind ui-queryString-search" data-source="@Html.GetCanAdminMemberGroupListJson()" data-custom-params="dataSourceFrom:'attr',valueField:'Id',textField:'Name'">
                        <option value="">所有用户名</option>
                    </select>
                    <select id="departmentId" class="ui-queryString form-control ui-selectBind ui-queryString-search" data-source="@Html.GetDepartmentListJson()" data-custom-params="dataSourceFrom:'attr',valueField:'Id',textField:'Name',onlySelectFinal:false,isTree:true">
                        <option value="">所有部门</option>
                    </select>
                    <select id="orderBy" class="ui-queryString form-control ui-queryString-search">
                        <option value="">默认排序</option>
                        <option value="id desc">按注册时间↓</option>
                        <option value="id asc">按注册时间↑</option>
                        <option value="lastDate desc">按最后登录时间↓</option>
                        <option value="logins desc">按登录次数↓</option>
                        @Html.Partial("~/Areas/E/Views/CustomForm/OrderFieldPartial.cshtml", allfieldListData)
                    </select>
                    <select id="s_field" name="s_field" class="btn-xs ui-queryString ui-setName form-control">
                        <option value="username">用户名</option>
                        <option value="email">邮箱</option>
                        <option value="mobile">手机号</option>
                        @Html.Partial("~/Areas/E/Views/CustomForm/SearchFieldPartial.cshtml", allfieldListData)
                        <option value="regDate" searchType="range" valueType="date">注册日期</option>
                        <option value="lastDate" searchType="range" valueType="date">登录日期</option>
                    </select>
                    <input type="hidden" class="ui-queryString" name="state" id="state" value="">
                    <input type="text" class="ui-queryString ui-keyword  form-control" style="width:140px" placeholder="输入关键词">
                    <button type="button" class="btn btn-default ui-queryString-search btn-sm">搜索</button>
                </div>
            </td>
        </tr>
    </table>
    <table border=0 cellpadding=0 cellspacing=0 class="table table-bordered" id="tb_list">
        <thead>
            <tr>
                <td align="center" width="30px"><input type="checkbox" title="选择" class="ui-selectInvert" data-custom-params="isReverse:false"></td>
                @*<td align="center">id</td>*@
                <td align="left">用户名</td>
                <td align="center">用户组</td>
                <td align="center">部门</td>
                <td align="center">邮箱</td>
                <td align="center">手机</td>
                @{
                    foreach (var item in fieldData)
                    {
                        <td align="center">@item.Remark</td>
                    }
                }
                <td align="center">注册时间@*[<a class="show-alldetail" href="javascript:void(0)">展开</a>]*@</td>
                <td align="center">状态</td>
                <td align="center">操作</td>
            </tr>
        </thead>

        <tr class="item list-template-item" id="item_{id}">
            <td align="center"><input type="checkbox" value="{id}" title="id:{id}" class="checked-item"></td>
            @*<td align="center">{id}</td>*@
            <td align="left">
                <div class="dropdown dropdown-hover">
                    <a class="dropdown-toggle" data-toggle="dropdown">
                        <span>{username}</span><span class="is_admin_{isAdmin}" title="管理员" style="color:#999999"></span>
                        <span class="caret"></span>
                    </a>
                    <div class="dropdown-menu item-detail" role="menu">
                        <table class="table" style="width:400px">
                            <tr>
                                <td>用户Id：{Id}</td>
                                <td>登录次数：{logins}</td>
                            </tr>
                            <tr>
                                <td>注册ip：{regIp}</td>
                                <td>最后登录Ip：{lastIp}</td>
                            </tr>
                            <tr>
                                <td>最后登录：{lastDate}</td>
                                <td><a  href="@Url.ActionUrl("GoMember")?uid={id}" target="_blank"><i class="fa fa-sign-in" aria-hidden="true"></i>进入会员中心</a></td>
                            </tr>
                        </table>
                    </div>
                </div>
            </td>
            <td align="center"><span class="ui-convertTextById sp_member_type" data-custom-params="">{memberGroupId}</span></td>
            <td align="center"><span class="ui-convertTextById sp_department" data-custom-params="">{departmentId}</span></td>
            <td align="center">{email}<i class="fa fa-check-square-o text-success show-{EmailState}" aria-hidden="true" title="已验证"></i></td>
            <td align="center">{mobile}<i class="fa fa-check-square-o text-success show-{MobileState}" aria-hidden="true" title="已验证"></i></td>
            @foreach (var item in fieldData)
            {
                int listWords = item.ListWords;
                int fieldId = item.Id;
                string field = item.Name;
                string fieldType = item.FieldType;
                string valueType = item.ValueType;
                string align = "center";
                if (valueType == "nvarchar" || valueType == "text")
                {
                    if (fieldType != "image")
                    {
                        align = "left";
                    }
                }
                <td align="@align">
                    @if (new string[] { "images", "files", "subtable" }.Contains(fieldType))
                    {
                        <span class="sp_@field sp_values" data-value-params="id:{id},fieldId:@fieldId,name:'@field',fieldType:'@fieldType',valueType:'@valueType',listWords:'@listWords'"><i class="fa fa-spinner fa-spin"></i></span>
                    }
                    else
                    {
                        <span class="sp_@field sp_values" data-value-params="id:{id},fieldId:@fieldId,name:'@field',fieldType:'@fieldType',valueType:'@valueType',listWords:'@listWords'">{@field}</span>
                    }
                </td>
            }
            <td align="center">{regDate}</td>
            <td align="center"><span class="sp_state">{state}</span></td>
            <td align="center">
                <input type="hidden" name="id" value="{id}" />
                <input type="hidden" name="guid" value="{guid}" />
                <div class="btn-group btn-group-sm">
                    <button class="btn btn-default btn-sm ui-dialog" data-custom-params="title:'{username}-修改', url:'@Url.ActionUrl("Edit")?id={id}&guid={guid}',width:'95%',height:'95%'">修改</button>
                    <button class="btn btn-default btn-sm ui-deleteSingle btn_is_admin_{is_admin}" data-custom-params="url:'@Url.ActionUrl("Delete")',callBack:'LoadData'">删除</button>
                </div>
            </td>
        </tr>
    </table>
</div>

<script type="text/javascript">
    $(".navTabs").children().InitTab(function () { LoadData() }); //初始化选项卡，并设置回调函数为LoadData()

    function LoadData() {
        var url = "@Url.ActionUrl("PageListData")";
        $("#tb_list").ListBind({ url: url, callBack: "LoadedInit" });
    }

    function LoadedInit() {
        $(".sp_member_type").ConvertText({valueField:"Id",textField:"Name"}, $("#memberGroupId").attr("data-source"));
        $(".sp_department").ConvertText({ emptyStr: "/" }, $("#departmentId").attr("data-source"));
        $(".show-detail").click(function () {
            $(this).next().show();
            $(this).hide();
        });

        $(".hide-detail").click(function () {
            $(this).parent().hide().prev().show();
        });
        var $is_admin_1 = $(".is_admin_1");
        $is_admin_1.text("[管]");
        $(".btn_is_admin_1").attr("disabled", "disabled");
        var $sp_state = $(".sp_state");
        $sp_state.each(function () {
            var $this = $(this);
            var txt = $this.text();
            switch (txt) {
                case "1":
                    $this.text("已审");
                    break;

                case "0":
                    $this.text("待审");
                    break;

                case "-1":
                    $this.text("禁用")
                    break;
            }
        });
        $(".dropdown-hover").DropdownHover();
        var $sp_values = $(".sp_values");
        $sp_values.each(function () {
            var $this = $(this);
            var thisHtml = $this.html();
            var params = $this.attr("data-value-params")
            params = ExtendObject(params);
            var parentId = params.id;
            var listWords = parseInt(params.listWords);
            var valueType = params.valueType;
            var fieldType = params.fieldType;
            var field = params.name;
            var fieldId = params.fieldId;
            if (fieldType == "subtable") {
                $.get("/E/SubData/Count", { _fieldId: fieldId, parentId: parentId }, function (data) { $this.text(data) });
            }
            else if (fieldType == "images" || fieldType == "files") {
                $.get("/E/AttachmentsData/Count", { _fieldId: fieldId, parentId: parentId }, function (data) { $this.text(data) });
            }
            else if (params.fieldType == "image" && thisHtml != "") {
                $this.html("<a href=\"" + thisHtml + "\" target=\"_blank\"><img src=\"" + thisHtml + "\" class=\"ui-thumbnail thumbnail\"></a>");
            }
            else if (valueType == "nvarchar" || valueType == "ntext" || valueType == "datetime") {
                if (thisHtml.length > listWords) {
                    if (field == "title") {
                        $this.html(thisHtml.Left(listWords) + "...");
                    }
                    else {
                        $this.html(thisHtml.Left(listWords));
                    }
                }
            }
        });
    }
    LoadData();



    //设置前检测
    function BeforeExecuting(ids, setName) {
        switch (setName) {
            case "lbzy":
                var params = { title: '类别转移', url: '@Url.ActionUrl("MemberGroupSet")?ids=' + ids + "&setName=lbzy", width: '600px', height: '80%' };
                IDialog(params);
                return false;
                break;

            case "bmsz":
                var params = { title: '部门设置', url: '@Url.ActionUrl("DepartmentSet")?ids=' + ids + "&setName=bmsz", width: '600px', height: '80%' };
                IDialog(params);
                return false;
                break;

            default:
                var Aids = ids.split(",");
                for (i = 0; i < Aids.length; i++) {
                    currentObj = $("#item_" + Aids[i]);
                    if (currentObj.attr("is_admin") == "1") {
                        Alert("管理员用户不能直接删除！");
                        return false;
                    }
                }
                break;
        }
        return true;
    }

</script>
